package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class HsgDamage extends BaseRepair {

    public HsgDamage(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void goAll(){
        List<Map<String, String>> listMap = new ArrayList<>();
        String dateStart = "2021-04-29";
        String dateEnd = "2021-05-02";
        listMap = getLogActivity(dateStart,dateEnd);
        DataMgr.addData(listMap);
//        DataMgr.makeTextData("",listMap);
    }

    public void go(){
        List<Map<String, String>> listMap = new ArrayList<>();
        String activityIdIn = "9008043,9008044,9008045,9008046,9008047,9008048,9008049,9008050,9008007,9008006,9007869,9007280,9007281,9007282,9007283";
        listMap = getLogActivity(activityIdIn);
        listMap = getAcData(listMap);
        DataMgr.addData(listMap);
//        DataMgr.makeTextData("",listMap);
    }

    private List<Map<String, String>> getLogActivity(String activityIdIn) {
        List<Map<String, String>> dataList = new ArrayList<>();
        if(StringUtils.isNullOrEmpty(activityIdIn)){
            return dataList;
        }
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select activityId,userId,SUM(damage) as allDamage,left(eventId,1) as eventType " +
                " from t_l_hsg_attack_log " +
                " where activityId in ("+activityIdIn+")" +
                " GROUP BY userId,left(eventId,1)";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
//            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("allDamage",checkNullToEmpty(rs.getString("allDamage")));
                temp.put("eventType",checkNullToEmpty(rs.getString("eventType")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLogActivity(String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select SUM(damage) as allDamage,count(DISTINCT userId) as allUser" +
                " from t_l_hsg_attack_log where attackTime >= ? AND attackTime <= ?";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("allDamage",checkNullToEmpty(rs.getString("allDamage")));
                temp.put("allUser",checkNullToEmpty(rs.getString("allUser")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getAcData(List<Map<String, String>> odataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_activity_condition WHERE userId = ? AND activityId = ?";
        try {
            for(Map<String,String> odata : odataList){
                call = getGameServer().getConnection().prepareCall(sql);
                call.setString(1, odata.get("userId"));
                call.setString(2, odata.get("activityId"));
                rs = call.executeQuery();
                if(rs.next()){
                    odata.put("integral",rs.getString("value"));
                    UserInfo userInfo = getUserInfo(odata.get("userId"));
                    odata.put("vipExp",String.valueOf(userInfo.getVipExp()));
                    BigInteger pAbility = getPatronsEarnSpeed(odata.get("userId"));
                    odata.put("门客总赚速",String.valueOf(pAbility));
                    dataList.add(odata);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private BigInteger getPatronsEarnSpeed(String userId){
        BigInteger allAbility = BigInteger.ZERO;
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_patrons WHERE userId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setString(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                allAbility = allAbility.add(new BigInteger(rs.getString("ability")));
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }

        return allAbility;
    }

}
